/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ec.tesis.prototiporecomendador.ngc.UserProfile;

import com.hp.hpl.jena.ontology.OntModel;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Xavier Riofrio
 */
public class NearestNeighbors {
        
    public static void addNeighbors(int idUser,int numNeigborgs, int numMovies,float disMax){
            Configuration conf = Configuration.getInstance();
            String [][] favoritos= new String[2][3];
            favoritos=ConnectionDB.selectFavorites(conf.getURL(),conf.getDBNAME(),conf.getDBUSER(),conf.getDBPASS(), idUser);
            
        try {
            ConnectionDB.deleteNeighborgs(conf, idUser);
               if(favoritos[1][2]!=null)
               {
                    ResultSet rs=ConnectionDB.getNearest(idUser,
                    favoritos[0][0],favoritos[1][0],favoritos[0][1],favoritos[1][1],favoritos[0][2],favoritos[1][2],
                    numNeigborgs,numMovies,  disMax,conf);
                    int flag=0;
                while(rs.next())
                {
                        String nei=rs.getString(1);
                        Float fl=rs.getFloat("suma");
                        ConnectionDB.insertNeighborg(conf,idUser,nei,fl);
                        flag=1;
                }
                    if(flag==1)
                System.out.println("Los vecinos han sido ingresados del usuario..."+idUser);
                    else
                        System.err.println("No tiene vecinos user "+idUser);
               
                        
               }else 
                   System.err.println("No tiene vecinos user "+idUser);
               
            }catch(Exception e){
			System.out.println("Se a producido una excepcion: "+e.getMessage());
	}
           
    }
    
    
    public static ArrayList<String> getNeighbors(int idUser)
    {
         Configuration conf = Configuration.getInstance();
         ArrayList<String> ite = ConnectionDB.selectNeighbors(conf, idUser);
          return ite;
        
    }
    
    
    public static void allNeightbors(int numNeighbor,int numMovies,float dis)
    {
//        try {
                Configuration conf = Configuration.getInstance();
//                FileWriter   f1 = new FileWriter("num-vecinos.dat");
//                BufferedWriter b1 = new BufferedWriter(f1);
//                float dis=8.6666666f;
//                int count=96;
//                while(count<100)
//                {
                
                 ArrayList<ArrayList<Calificacion>> agrupados = UserDataFileReader.RatingsReadLines(conf.getDBRATINGS());
            Iterator<ArrayList<Calificacion>> ite = agrupados.iterator();
                   //recorro los usuarios
            while (ite.hasNext()) {
                ArrayList<Calificacion> grupo = ite.next();
                if (!grupo.isEmpty()) {
                    
                            addNeighbors(grupo.get(0).getIdUser(),numNeighbor,numMovies,dis);
                      
//                    double numero=ConnectionDB.countNeighborgs(conf);
//                    b1.write((dis*count)+":"+numero);
//                    b1.newLine();
//                    b1.flush();
//                    count=count+2;
//                }
                }
            }
                System.err.println("USUARIOS CON VECINOS");
                
////                f1.close();
//            } catch (IOException ex) {
//                Logger.getLogger(NearestNeighbors.class.getName()).log(Level.SEVERE, null, ex);
//            } 
//                    
        
    }
    
    
    
    
    
}
